/**
 * Created by Administrator on 2017/9/16 0016.
 */
$(function(){
    var $name = $(".name");
    var $uName = $(".uName");
    var $pwd = $(".pwd");
    var $conPwd = $(".conPwd");
    var $yzm = $(".yzm");
    var $pNumber = $(".pNumber");
    var $sms_code = $(".sms-code");


    var $pRe = $(".pRe");
    var $eRe = $(".eRe");
    var $phone = $("#phone");
    var $email = $("#email");

    var $xieYi = $(".xieyi");
    var $cha = $(".cha");
    var $shade = $(".shade");
    var $agreeInfo = $(".agreeInfo");

    var $btn_getsms = $(".btn-getsms");
    var $chImage = $("#chImage");
    var $imgYzm = $(".imgYzm");
    var codes = ['W', 'q', 'E', 'r', 'T', 'Y', 'u', 'i', 'o', 'P', 'a', 'S', 'd', 'f', 'g', 'h', 'j', 'k', 'L', 'z', 'X', 'c', 'v', 'b', 'N', 'm', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'];

    //图片验证码

    $chImage.on('click',function(){
        alert("asfgagb")
        $(this).parent("em").prev("img").attr("src","/kaptcha.jpg?t=" + new Date().getTime());
    })

    $btn_getsms.on('click',function(){

        var flag = $("#regForm").validate().element($pNumber);
        var flag2 = $("#regForm").validate().element($yzm);
        if(!flag || !flag2) return false;

        var phone = $pNumber.val();
        var code = $yzm.val();

        $btn_getsms.prop("disabled","disabled");
        $btn_getsms.css("background","black")

        $.ajax({
            url:"/reg/sendMsg",
            type:"get",
            dataType: "json",
            data: {phone:phone, code :code},
            success:function(data) {
                if(data.code == 1) {
                    var count = 60;
                    var timer = setInterval(function(){
                        if(count == 0) {
                            $btn_getsms.text("获取验证码");
                            $btn_getsms.css("background","#4ba7e0");
                            $btn_getsms.prop("disabled","");
                            clearInterval(timer);
                        }else {
                            $btn_getsms.text((count--) + "获取验证码");
                        }
                    },1000)
                    layer.msg(data.msg);
                }else{
                    $btn_getsms.prop("disabled","");
                    $btn_getsms.css("background","#4ba7e0");
                    layer.msg(data.msg);
                }

            },
            error: function(){
                $btn_getsms.prop("disabled","");
                $btn_getsms.css("background","#4ba7e0");
                layer.msg("服务器忙，请稍后再试！");

            }
        });
    })

    // 切换注册页面
    $pRe.on('click',function(){
        $(this).addClass("active");
        $eRe.removeClass("active")
        $phone.show();
        $email.hide();
    })

    $eRe.on('click',function(){
        $(this).addClass("active");
        $pRe.removeClass("active")
        $phone.hide();
        $email.show();
    })

    //弹出遮罩和协议
        $xieYi.on('click',function () {
            layer.open({
                type:2,
                closeBtn:1,
                title:['协议','font-size:22px;text-align:center'],
                area:['700px','800px'],
                content:'regInfo.html',
                btn:'同意并继续',
                yes:function(index){
                    $("input[name = agree]").prop("checked",true);
                    layer.close(index);
                }
            })
        })

    //  获取验证码


   /* $btn_getsms.on('click',function(){
        var time = 10;
        $btn_getsms.prop("disabled","disabled");
        $btn_getsms.css("background","black")
        var dis = setInterval(function(){
            $btn_getsms.text(time+"s后重新获取");
            if(time<0){
                $btn_getsms.text("获取验证码");
                $btn_getsms.css("background","#4ba7e0");
                $btn_getsms.prop("disabled","");
                clearInterval(djs);
            }
            time--;
        },1000)
    })*/

    // 当文本框内有内容时显示cha,清除提示信息，没有内容时隐藏cha ;

    function fun2(){
        $(this).next().removeClass("hide")
        $(this).next().next().addClass("hide")
        if(!$(this).val()){
            $(this).next().addClass("hide")
        }
    }
    $name.on('input',fun2);
    $uName.on('input',fun2);
    $pwd.on('input', fun2);
    $conPwd.on('input', fun2);
    $yzm.on('input', fun2);
    $pNumber.on('input', fun2);

    $sms_code.on('input', function(){
        var $this = $(this);
        $this.next().removeClass("hide")
        $this.next().next().next().addClass("hide")
        if(!$this.val()){
            $this.next().addClass("hide")
        }
    });


    // 点x时清除文本框内的内容并隐藏x
    $(".u-clear").on('click',function(e){
        var $this = $(this)
        $this.prev().val("")
        $this.addClass("hide")
    });
})